#ifndef __MUTEXLOCK_H
#define __MUTEXLOCK_H

#include "noncopyable.hpp"
#include <pthread.h>
namespace moqi
{
class MutexLock:public Noncopyable
{
public:
    MutexLock();
    ~MutexLock();
    void lock();
    void unlock();
    pthread_mutex_t* getMutex();
private:
    pthread_mutex_t _mutex;
};

class MutexLockGuard
{
public:
    explicit MutexLockGuard(MutexLock &m)
        : _mutex(m), _locked(true)
    {
        _mutex.lock();
    }

    ~MutexLockGuard()
    {
        if (_locked) {
            _mutex.unlock();
        }
    }

    void unlock()
    {
        if (_locked) {
            _mutex.unlock();
            _locked = false;
        }
    }

    void lock()
    {
        if (!_locked) {
            _mutex.lock();
            _locked = true;
        }
    }

private:
    MutexLock &_mutex;
    bool _locked; // 标记锁的状态
};



}
#endif //!__MUTEXLOCK_H
